<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\token\media;

use app\enum\CommonEnum;
use app\model\MediaTokenModel;
use app\service\kuaishouesp\TokenService;
use app\service\token\MediaService;
use app\service\token\TokenInterface;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Cache;

class KuaiShouEspService extends MediaService implements TokenInterface
{
    protected int $mediaId = CommonEnum::KS_ESP_MEDIA;

    /**
     * 验证token和账户是否有效.
     * @param MediaTokenModel $model 模型
     * @param int $accountId 账户id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function checkTokenAccount(MediaTokenModel $model, int $accountId = 0): bool
    {
        $redis = Cache::store('redis')->handler();
        $token = $redis->get($model->getAttr('redis_key'));
        if (! $token) {
            return self::setError('token不存在');
        }
        return TokenService::getAccountInfo($accountId, $token);
    }

    /**
     * 根据refresh_token刷新token.
     * @param string $appId appid
     * @param string $appSecret secret
     * @param string $refreshToken refresh_token
     */
    public function sendRefreshToken(string $appId = '', string $appSecret = '', string $refreshToken = '')
    {
        return TokenService::refreshToken($appId, $appSecret, $refreshToken);
    }
}
